/**
 * Created by wenchuan on 2018/5/17.
 */
define(function () {

    var self,
        app = win.app,
        detailData,
        CHARACTERISTICS = [],
        selectedCharacter = [],
        comments = [],
        other = {
            GWAM016: '',
            GWAM017: ''
        },
        // 存放可以修改的属性
        updateData = {
            GWCM001: '',
            GWCM002: '',
            GWCM003: '',
            GWCM004: '',
            GWCM050: '',
            GWAM015: '',
            GWAMl003: ''
        },
        refreshData = {
            key: 0, //评价key
            status: 0,
            count: 0,
            count1: 0,
            count2: 0,
            count3: 0,
            pv: 0
        },
        pageParameters,
        pageUi
    ;

    var ClassesInfo = app.Module.extend({

        init: function () {
            self = this;
            pageParameters = app.pageParameters['zpclassesinfo_page'];
            detailData = pageParameters.order;

            app.extend(updateData, detailData);

            self._super({}, win);// 放在后边
        },
        getSelectedCharacteristic: function () {
            app.ajax({
                xurl: "api/wx/gwat100/getSelectedCharacteristic",
                xdata: {
                    GWCNDOCNO: detailData.GWCMDOCNO,
                    GWCNSEQ2: detailData.GWCMSEQ
                },
                cb: function (result) {
                    if (result.ok) {
                        var datas = result.data.data;
                        selectedCharacter.pushArray(datas, true);

                        selectedCharacter.forEach(function (v) {
                            CHARACTERISTICS.filterKV({
                                GWAN002: v.GWCN002
                            }).addMap({
                                selectVal: v.GWCN003
                            }, true);
                        });
                    }
                    self.getGWAMOther();
                }
            });
        },
        getCharacteristic: function () {
            app.ajax({
                xurl: "api/wx/gwat100/getCharacteristic",
                xdata: {
                    GWAN001: detailData.GWCM001
                },
                cb: function (result) {
                    if (result.ok) {
                        var datas = result.data.data;
                        datas.forEach(function (v) {
                            if (v.child && v.child.length > 0) {
                                v.selectVal = v.child[0].GWAJ002;
                            }

                        });
                        CHARACTERISTICS.pushArray(datas);
                    }
                    self.getSelectedCharacteristic();
                }
            });
        },
        getGWAMOther: function () {
            app.ajax({
                xurl: "api/wx/gwat100/getGwamOther",
                xdata: {
                    GWAM001: updateData.GWCM001
                },
                cb: function (result) {
                    if (result.ok) {
                        var data = result.data.data;
                        app.extend(other, data);
                    }
                }
            });
        },
        getGwcwCount: function () {
            app.ajax({
                xurl: "api/wx/gwat100/getGwcwCount",
                xdata: {
                    GWCW020: updateData.GWCM001,
                    GWAM005: 1, //料号类型 1子料号
                    status: refreshData.status // 0全部 1当前商品评论用于子料号
                },
                cb: function (result) {
                    if (result.ok) {
                        var data = result.data.count;
                        app.extend(refreshData, result.data.count);
                        if (data.count > 0) {
                            refreshData.pv = (refreshData.count1 / refreshData.count * 100).toFixed(2);
                        } else {
                            refreshData.pv = 0;
                        }
                    }
                }
            });
        },
        _initRefresh: function () { // 实例化刷新组件
            if (self._refreshIds.length == 0) {
                self._refreshIds = ['scroll3'];
                self._refreshVueData = refreshData;
                self._refreshVueMethods = {
                    statusChange: function () {
                        self._refresh();
                        self.getGwcwCount();
                    },
                    getpj: function (val) {
                        refreshData.key = val;
                        self._refresh();
                    }
                };
                this._super(win);
            }else{
                self._refresh();
            }

            self.getGwcwCount();
        },
        _refreshCb: function (goods) {// 刷新组件回调函授
            var _super = this._super;
            app.ajax({
                xurl: "api/wx/gwat100/getGwcw",
                xdata: {
                    page:goods.page+1,
                    limit: goods.limit,
                    GWCW020: updateData.GWCM001,
                    key: refreshData.key,//评论级别
                    GWAM005: 1, //料号类型
                    status: refreshData.status // 0全部 1当前商品评论用于子料号
                },
                cb: function (result) {
                    if (result.ok) {
                        var datas = result.data.data;

                        _super({
                            obj: goods,
                            data: datas,
                            nodata: '暂无评论信息...',
                            nodata_img: 'noneComment.png'
                        });
                    }
                }
            });
        },
        _lazy: function () {

            //self.getCharacteristic();

            self.getGWAMOther();

            app.taps.call(win, [{
                o: 'btnSave1',
                cb: function (e) {

                    app.extend(detailData, updateData);
                    app.back(1);
                }
            }]);
        },
        _ready: function () {
            var scroll1Vue = new Vue({
                el: app.getByid(doc, 'scroll1'),
                data: {
                    item: detailData,
                    CHARACTERISTICS: CHARACTERISTICS,
                    comments: comments,
                    ud: updateData
                },
                methods: {
                    minus: function () {
                        if (updateData.GWCM003 > 1) {
                            updateData.GWCM003--;
                        }
                    },
                    plus: function () {
                        updateData.GWCM003++;
                    },
                    characterChange: function (val, val1) {
                        val.selectVal = val1.GWAJ002;
                    }
                }
            });
            Vue.component('commentsitem', {
                template: app.getByid(doc, 'comments-template'),
                props: {
                    model: Array
                },
                methods: {}
            });

            new Vue({
                el: app.getByid(doc, 'scroll2'),
                data: other,
                methods: {
                    provinceChange: function () {

                    }
                }
            });

            app.getCharacteristic({
                gwan001_: updateData.GWCM001,
                gwam005_: 1,
                _vue: scroll1Vue,
                cb: function (result) {
                    //用到缓存 会调用2次，不要放跟特征值无关的逻辑

                },
                changeCb: function (obj) {
                    updateData.GWCM001 = obj.GWAM001;
                    updateData.GWAMl003 = obj.GWAMl003;
                    updateData.GWCM002 = obj.GWAM008;//销售单位
                    updateData.GWCM004 = Number(obj.GWAP100);//单价
                    updateData.GWAM015 = obj.GWAM015;
                    self.getGWAMOther();
                }
            });

            $('.wf-infoTab ul li').on('tap', function () {
                $(this).addClass('active').siblings('li').removeClass('active');
                $(this).parents('.wf-infoTab').find('.tabCont .group').eq($(this).index()).show().siblings('.group').hide();
            });

            doc.getElementById('slider').addEventListener('slide', function (e) {
                if (e.detail.slideNumber == 2) {
                    self._initRefresh(win);
                }
            });
        }
    });

    new ClassesInfo();

});